//
//  MeBgImageViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/5/16.
//

#import "MeBgImageViewController.h"

@interface MeBgImageViewController ()<TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic , strong) SQCustomButton * bgImageView;
@end

@implementation MeBgImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"个人背景");
    WS(weakSelf);
    [self zx_setRightBtnWithText:SQNSLocalString(@"提交") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf SQ_rightTopClick];
    }];
    self.zx_navRightBtn.hidden = YES;
    [self zx_leftClickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        if ( weakSelf.zx_navRightBtn.hidden == NO) {
            [weakSelf SQ_tuichuButtonClick];
        } else {
            [weakSelf SQ_popViewVC];
        }
    }];
}

- (void)SQ_UIViewForSelfView{
    if ([self.aboutString isNotEmpty]) {
        [self.bgImageView.SQBackimageView sd_setImageWithURL:SQURLImage(self.aboutString)];
        self.bgImageView.imageView.hidden = YES;
    }
    [self.view addSubview:self.bgImageView];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    WS(weakSelf);
    [self.bgImageView setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_headerButtonClick];
    }];
}


-(void)SQ_headerButtonClick{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"选择照片") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_leftButtonClick];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_rightButtonClick];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [[Manager SQ_getCurrentViewController] presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_leftButtonClick{
    WS(weakSelf);
    _imagePickerVc = [[UIImagePickerController alloc] init];
    _imagePickerVc.delegate = self;
    _imagePickerVc.allowsEditing = YES;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        weakSelf.imagePickerVc.sourceType = sourceType;
        [[Manager SQ_getCurrentViewController] presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    } else {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        [weakSelf SQ_caijian:photo];
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}


-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    vc.allowPickingVideo = NO;
    vc.scaleAspectFillCrop = NO;
    [[Manager SQ_getCurrentViewController] presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        for (UIImage * image in photos) {
            [weakSelf SQ_caijian:image];
        }
    }];
}

//添加图片到列表
-(void)SQ_caijian:(UIImage*)image {
    
    self.bgImageView.SQBackimageView.image = image;
    self.bgImageView.imageView.hidden = YES;
    self.zx_navRightBtn.hidden = NO;
    self.zx_navBarBackgroundColor = UIColor.blackColor;
}

-(void)SQ_tuichuButtonClick{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"是否退出") message:SQNSLocalString(@"确认退出将无法保存已填写内容") preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"确认退出") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_popViewVC];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co2];
    [[Manager SQ_getCurrentViewController] presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_rightTopClick{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:self.bgImageView.SQBackimageView.image successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_ziwojieshao:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_ziwojieshao:(NSString*)header{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:header forKey:@"cover"];
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"设置成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self SQ_popViewVC];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (SQCustomButton *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(40, 40) midmargin:0];
        _bgImageView.SQBackimageView.contentMode = UIViewContentModeScaleAspectFill;
        _bgImageView.clipsToBounds = YES;
        _bgImageView.image = SQGetImage(@"添加");
    }
    return _bgImageView;
}
@end
